package com.hulu.yec.sys.role.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.hulu.yec.common.Constant;
import com.hulu.yec.common.exception.DaoException;
import com.hulu.yec.sys.entity.MenuEntity;
import com.hulu.yec.sys.role.dao.RoleDao;
import com.hulu.yec.sys.role.entity.RoleEntity;
import com.hulu.yec.sys.service.SysDao;
import com.hulu.yec.sys.user.entity.SysUserEntity;

@WebServlet("/role/RoleServlet")
public class RoleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RoleDao roleDao = new RoleDao();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constant.ENCODING_UTF8);
		String command = request.getParameter("command");
		if ("list".equals(command)) {
			list(request, response);
		} else if ("add".equals(command)) {
			add(request, response);
		} else if ("changeState".equals(command)) {
			changeState(request, response);
		} else if ("preUpdate".equals(command)) {
			preUpdate(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else if ("preadd".equals(command)) {
			preadd(request, response);
		} else if ("onlyRoleName".equals(command)) {
			onlyRoleName(request, response);
		} else if ("toGrant".equals(command)) {
			toGrant(request, response);
		} else if ("grant".equals(command)) {
			grantRightById(request, response);
		}else {
			throw new RuntimeException("请求非法!!!");
		}
	}

	/**
	 * 授权操作
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void grantRightById(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String idStr= request.getParameter("roleId");
		if(StringUtils.isNoneBlank(idStr)){
			String paramStr = request.getParameter("menuString");
			String [] menuArray = paramStr.split(",");
			
			
			roleDao.deleteRightById(idStr);
			if(roleDao.addRightById(idStr,menuArray)){
				response.sendRedirect("RoleServlet?command=list");
			}
		}
	}


	/**
	 * 初始化授权页面
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void toGrant(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr= request.getParameter("id");
		if(StringUtils.isNoneBlank(idStr)){
			int id = Integer.parseInt(idStr);
			List<MenuEntity> mainMenuList = roleDao.getMainMenu(id) ;
			List<MenuEntity> childMenuList = roleDao.getChildMenu(id) ;
			List<MenuEntity> allMainMenuList = roleDao.getAllMainMenu() ;
			List<MenuEntity> allChildMenuList = roleDao.getAllChildMenu() ;
			request.setAttribute("allMainMenuList",allMainMenuList);
			request.setAttribute("allChildMenuList",allChildMenuList);
			request.setAttribute("roleId", id);
			request.setAttribute("mainMenuList",mainMenuList);
			request.setAttribute("childMenuList",childMenuList);
			request.getRequestDispatcher("/WEB-INF/view/sys/role/grantRight.jsp").forward(request, response);
		}
	}

	/**
	 * 查询角色管理信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @throws DaoException
	 */
	private void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取参数-条件查询参数
		int state = 0;
		String stateStr = request.getParameter("state");
		if (stateStr != null) {
			state = Integer.parseInt(stateStr);
		}
		RoleEntity role = new RoleEntity();
		role.setState(state);
		// 业务逻辑
		List<RoleEntity> roleList = roleDao.list(role);
		request.setAttribute("state", state);
		request.setAttribute("roleList", roleList);
		// 转发
		request.getRequestDispatcher("/WEB-INF/view/sys/role/role-list.jsp")
				.forward(request, response);
	}

	// /**
	// * 状态更新
	// *
	// * @param request
	// * @param response
	// * @throws ServletException
	// * @throws IOException
	// * @throws DaoException
	// */
	// private void updateState(HttpServletRequest request,
	// HttpServletResponse response) throws ServletException, IOException,
	// DaoException {
	// // 获取参数
	// int id = Integer.parseInt(request.getParameter("id"));
	// int state = Integer.parseInt(request.getParameter("state"));
	// // 调用逻辑
	// int reState = 1;
	// if (state == 1) {
	// reState = 2;
	// }
	// int row = roleDao.updateState(id, reState);
	// // 转向
	// if (row == 1) {
	// request.setAttribute("result", 1);
	// request.getRequestDispatcher("/RoleServlet?command=list").forward(
	// request, response);
	// } else {
	// request.setAttribute("result", 0);
	// request.getRequestDispatcher("/RoleServlet?command=list").forward(
	// request, response);
	// }
	// }

	/**
	 * 状态更新
	 * 
	 * @param request
	 * @param response
	 * @throws DaoException
	 * @throws IOException
	 */
	private void changeState(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String inStr = request.getParameter("id");
		System.out.println(inStr);
		int id = 0;
		if (StringUtils.isNoneBlank(inStr)) {
			id = Integer.parseInt(inStr);
		} else {
			throw new RuntimeException("错误请求");
		}
		int reState = roleDao.getState(id);
		if (reState == 1) {
			reState = 2;
		} else if (reState == 2) {
			reState = 1;
		}
		roleDao.updateState(id, reState);
		// 转向
		response.setCharacterEncoding(Constant.ENCODING_UTF8);
		PrintWriter out = response.getWriter();
		int state = roleDao.getState(id);
		out.print(state);
		out.close();
	}

	/**
	 * 新增前跳转
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void preadd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/view/sys/role/role-add.jsp")
				.forward(request, response);
	}

	/**
	 * 角色新增
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 * @throws DaoException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取参数
		String name = request.getParameter("name");
		String memo = request.getParameter("memo");
		// 调用业务逻辑
		SysUserEntity user = (SysUserEntity) request.getSession().getAttribute(
				"user");
		int id = user.getId();
		int row = 0;
		row = roleDao.add(id, name, memo);
		// 转向
		if (row == 1) {
			request.getRequestDispatcher("/role/RoleServlet?command=list")
					.forward(request, response);
		} else {
			request.getRequestDispatcher("/WEB-INF/view/sys/role/role-add.jsp")
					.forward(request, response);
		}
	}

	/**
	 * 修改角色信息回显
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 * @throws DaoException
	 */
	private void preUpdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 获取参数
		int id = Integer.parseInt(request.getParameter("id"));
		// 调用业务逻辑
		List<RoleEntity> roleList = RoleDao.prelist(id);
		// 转向
		request.setAttribute("roleList", roleList);
		request.getRequestDispatcher("/WEB-INF/view/sys/role/role-update.jsp")
				.forward(request, response);
	}

	/**
	 * 角色修改
	 * 
	 * @param request
	 * @param response
	 * @throws DaoException
	 * @throws IOException
	 * @throws ServletException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取参数
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		int state = Integer.parseInt(request.getParameter("state"));
		String memo = request.getParameter("memo");
		// 调用业务逻辑
		int row = roleDao.update(id, name, state, memo);
		// 转向
		if (row == 1) {
			request.getRequestDispatcher("/role/RoleServlet?command=list")
					.forward(request, response);
		} else {
			request.getRequestDispatcher(
					"/WEB-INF/view/sys/role/role-update.jsp").forward(request,
					response);
		}
	}

	/**
	 * 角色名字唯一验证
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void onlyRoleName(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String name = request.getParameter("name");
		roleDao.onlyRoleName(name, request, response);
	}
}
